package com.erp.servlet;

import com.erp.bean.*;
import com.erp.service.*;
import com.erp.service.impl.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

/**
 * @author mtf
 * 去评价控制器
 */
@WebServlet("/already.action")
public class EvaluateAlreadyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建业务逻辑对象
        EvaluateService evaluateService = new EvaluateServiceImpl();
        OrderService orderService = new OrderServiceImpl();
        SupplierService supplierService = new SupplierServiceImpl();
        CommodityService commodityService = new CommodityServiceImpl();
        //接收信息
        String oid = req.getParameter("oid");
        String type = req.getParameter("type");
        Order order = orderService.findById(oid);
        Commodity commodity = commodityService.findByGId(order.getGid());
        if (type.equals("1")){
            Supplier supplier = supplierService.findBySupid(commodity.getSupid());
            req.setAttribute("supplier",supplier);
            req.setAttribute("order",order);
            req.getRequestDispatcher("evaluatealready.jsp").forward(req,resp);
        }else if (type.equals("2")){
            //获取表单提交的参数
            String content = req.getParameter("content");
            //创建评价对象
            Evaluate evaluate1 = new Evaluate();
            evaluate1.setSupid(commodity.getSupid());
            evaluate1.setOid(oid);
            evaluate1.setContent(content);
            evaluate1.setEvatime(new Timestamp(new Date().getTime()));
            //调用add
            evaluateService.add(evaluate1);
            //修改订单状态为已完成
            orderService.updateBatch(7,oid);
            //调用报表的add()
            ReportForms forms = new ReportForms();
            forms.setOid(oid);
            ReportFormService formService = new ReportFormServiceImpl();
            formService.add(forms);
            resp.sendRedirect("evaluatelist.action");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}










